package ls.test;

import ls.dao.CourseDAO;
import ls.dao.DAOFactory;
import ls.dao.StudentDAO;
import ls.entity.Course;
import ls.entity.Student;

import java.util.List;
import java.util.Scanner;

public class ChooseStudent {
    public int chooseStudent(String teacher_id)
    {
        CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
        List<Course> courseList = courseDAO.chooseStu(teacher_id);
        String course_name = null;
        if(courseList.size() == 0)
        {
            System.out.println("教师您好，您暂时还没有需要选择助教的课程！");
            int m = middle(teacher_id);
            if(m==0) return 0;
        }
        else if(courseList.size() == 1)
        {
            course_name = courseList.get(0).getCourse_name();
            impl(courseList,course_name,teacher_id);
        }
        else
        {
            System.out.println("经检测，您需要选择助教的课程不止1门，课程如下：");
            course(courseList);
            System.out.println("请输入您要选助教的课程名：");
            Scanner scanner = new Scanner(System.in);
            course_name = scanner.next();
            boolean b = isCourse(courseList,course_name);
            if(b)
            {
                impl(courseList,course_name,teacher_id);
            }
            else {
                System.out.println("输入的课程名不在上面的List中！");
                int m = middle(teacher_id);
                if(m==0) return 0;
            }
        }
        return 0;
    }
    public int impl(List<Course> courseList,String course_name,String teacher_id)
    {
        StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
        CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
        List<Student> studentList = studentDAO.choose(course_name);
        if (studentList.size() == 0)
        {
            System.out.println("暂时没有选择"+course_name+"课程为助教志愿的，请您耐心等待");
            int m = middle(teacher_id);
            if(m==0) return 0;
        }
        System.out.println("填报了"+course_name+"课程助教志愿的学生信息如下：");
        student(studentList);
        System.out.println("请选择您想要成为其助教的学生学号：");
        Scanner scanner = new Scanner(System.in);
        String student_id = scanner.next();
        boolean b = isStudent(studentList,student_id);
        if (b)
        {
            System.out.println("选择成功！");
            Student student = new Student();
            student.setStudent_id(student_id);
            student.setState("助教中");
            student.setCourse(course_name);
            studentDAO.updateStudentState(student);  //更新学生助教状态和助教课程
            Course course = new Course();
            course.setStudent_id(student_id);
            course.setCourse_state("有助教");
            course.setCourse_name(course_name);
            courseDAO.updateCourse(course);    // 更新课程状态
            int m = middle(teacher_id);
            if(m==0) return 0;
        }
        else {
            System.out.println("您输入的学号不在上面List中！");
            int m = middle(teacher_id);
            if(m==0) return 0;
        }
        return 0;
    }
    public int middle(String teacher_id)
    {
        System.out.println("请选择下面要进行的操作：");
        System.out.println("1.课程信息录入");
        System.out.println("2.选择助教学生");
        System.out.println("3.审核助教评定表");
        System.out.println("4.查看已录入的我的全部课程");
        System.out.println("5.查看我已处理的全部评定表");
        System.out.println("6.退出系统");
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        if(a == 1)
        {
            CreateCourse course = new CreateCourse();
            course.create(teacher_id);
        }
        else if(a == 2)
        {
            chooseStudent(teacher_id);
        }
        else if(a == 3)
        {
            TeacherEvaluate te = new TeacherEvaluate();
            te.teacherEvaluate(teacher_id);
        }
        else if(a == 4)
        {
            showCourse(teacher_id);
        }
        else if(a == 5)
        {
            TeacherEvaluate teacherEvaluate = new TeacherEvaluate();
            teacherEvaluate.getMyEvaluate(teacher_id);
        }
        else if(a == 6)
        {
            return 0;
        }
        else
        {
            System.out.println("输入不合法！");
            middle(teacher_id);
        }
        return 0;
    }
    public void course(List<Course> courseList)
    {
        for (int i = 0;i < courseList.size();i++)
        {
            Course course = courseList.get(i);
            System.out.print("课程名："+course.getCourse_name());
            System.out.print("  授课教师："+course.getCourse_teacher());
            System.out.print("  课程性质："+course.getCourse_property());
            System.out.print("  课程对象："+course.getCourse_student());
            System.out.print("  学时数："+course.getCourse_hour());
            System.out.print("  选课人数："+course.getNumber());
            System.out.print("  课程状态："+course.getCourse_state());
            System.out.print("  助教学号："+course.getStudent_id());
            System.out.println();
        }
    }
    public void student(List<Student> studentList)
    {
        for(int i = 0;i < studentList.size();i++)
        {
            Student student = studentList.get(i);
            System.out.print("学生学号："+student.getStudent_id());
            System.out.print("学生姓名："+student.getStudent_name());
            System.out.print("  专业："+student.getStudent_major());
            System.out.print("  学历："+student.getStudent_education());
            System.out.print("  第一志愿："+student.getFirstVol());
            System.out.print("  第二志愿："+student.getSecondVol());
            System.out.print("  导师："+student.getTeacher());
            System.out.print("  参加学术交流次数："+student.getCommunicate_num());
            System.out.print("  参加项目数量："+student.getProject_num());
            System.out.println();
        }
    }
    public boolean isStudent(List<Student> studentList,String student_id)
    {
        int flag = 0;
        for (int i = 0;i < studentList.size();i++)
        {
            if(student_id.equals(studentList.get(i).getStudent_id()))
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
            return true;
        else return false;
    }
    public boolean isCourse(List<Course> courseList,String course_name)
    {
        int flag = 0;
        for (int i = 0;i < courseList.size();i++)
        {
            if (course_name.equals(courseList.get(i).getCourse_name()))
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
            return true;
        else return false;
    }
    public void showCourse(String teacher_id)
    {
        CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
        List<Course> courseList = courseDAO.getMyCourse(teacher_id);
        if(courseList.size() == 0)
            System.out.println("您暂时还没有录入的课程！");
        else
            course(courseList);
        middle(teacher_id);
    }
}
